package pc

import (

	// "tcsc/sword"

	"fmt"
	"os"
	"strconv"
	"strings"
	"sync"
	"tcsc/controllers/mobile"
	"tcsc/models"
	"tcsc/sword"
	"time"

	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

func IndexCache(uid string) {
	var cache = make(map[string]interface{})

	cache["ad"] = models.Query(`select a.url as urls,b.pathname from mt_ad a left join mt_resources b on a.img=b.id where a.type=1 and a.status=1 
		and a.platform=31 and a.channel=59 order by a.sort desc`)
	cache["brand"] = models.Query(`select a.id,b.pathname,a.name from mt_brand a left join mt_resources b on a.logo=b.id where a.status=1 and 
		a.censoring=1 and a.is_show=1 order by a.recommend desc,a.sort desc limit 0,23`)

	// cache["good"] = mobile.ProductList(`select a.id,a.isvip,a.title,a.price,a.sellnum,a.keywords,a.issellnum,b.pathname from mt_product a left
	// 	join mt_resources b on a.img=b.id where a.censoring=1 and a.status=1 and a.good=1 order by a.sort desc limit 5`, uid)

	// cache["recom"] = mobile.ProductList(`select a.id,a.isvip,a.title,a.price,a.sellnum,a.keywords,a.issellnum,b.pathname from mt_product a left
	// 	join mt_resources b on a.img=b.id where a.censoring=1 and a.status=1 and a.recom=1 order by a.sort desc limit 5`, uid)

	// cache["mbuy"] = mobile.ProductList(`select a.id,a.isvip,a.title,a.price,a.sellnum,a.keywords,a.issellnum,b.pathname from mt_product a left
	// 	join mt_resources b on a.img=b.id where a.censoring=1 and a.status=1 and a.mbuy=1 order by a.sort desc limit 5`, uid)

	// cache["recommend"] = mobile.ProductList(`select a.id,a.isvip,a.title,a.price,a.sellnum,a.keywords,a.issellnum,b.pathname from mt_product a left
	// 	join mt_resources b on a.img=b.id where a.censoring=1 and a.status=1 and a.recommend=1 order by a.sort desc limit 5`, uid)

	// cache["newproduct"] = mobile.ProductList(`select a.id,a.isvip,a.title,a.price,a.sellnum,a.keywords,a.issellnum,b.pathname from mt_product a left
	// 	join mt_resources b on a.img=b.id where a.censoring=1 and a.status=1 order by a.addtime desc limit 5`, uid)

	cache["columnhot"] = models.Query(`select a.id,a.name,a.icon from mt_column a where
		a.type='p' and a.status=1 and a.pid=0 and a.gid=0 and a.hot=1 order by a.sort desc`)

	Bm.Put("index", cache, 600*time.Second)
}

func ProgramCache(gid string) {
	var cache = make(map[string]interface{})
	program := models.Query(`select a.id,a.name,a.intr,a.pid,b.pathname from mt_program a left join mt_resources b on a.img=b.id where a.status=1 and a.gid=` + gid + ` order by sort desc`)

	//for i, v := range program {
	//	if v["pid"] != nil {
	//		// pid := fmt.Sprint(v["pid"])
	//		// product := models.Query(`select a.* from mt_product a where a.censoring=1 and a.status=1 and a.id in(` + pid + `)`)
	//		// if product == nil {
	//		// 	program[i] = nil
	//		// }
	//	} else {
	//		program[i] = nil
	//	}
	//}
	cache["program"] = program

	Bm.Put("indexProgram"+gid, cache, 600*time.Second)
}

type Index struct {
	BaseController
}

func (c *Index) Get() {
	shopWhere := ""
	gbuyWhere := "a.gid = 0 and"
	gid := "0"
	if c.ShopId != "" {
		shopWhere = " and a.gid = " + c.ShopId + " "
		gbuyWhere = "a.gid = " + c.ShopId + " and"
		gid = c.ShopId
	} else {
		shopWhere = " and a.gid not in (" + c.ShopIdAll + ") "
	}
	//Bm.ClearAll()
	if Bm.IsExist("index") {
		c.Data["index"] = Bm.Get("index")
	} else {
		IndexCache(c.Uid)
		c.Data["index"] = Bm.Get("index")
	}
	if Bm.IsExist("indexProgram" + gid) {
		c.Data["indexprogram"] = Bm.Get("indexProgram" + gid)
	} else {
		ProgramCache(gid)
		c.Data["indexprogram"] = Bm.Get("indexProgram" + gid)
	}
	//aa := Bm.Get("indexProgram" + gid)
	//beego.Debug(aa)
	column := models.Query(`
		select a.id,a.name,a.icon,b.pathname,a.hot 
		from mt_column a 
		left join mt_resources b on a.img=b.id 
		where a.status=1 and a.pid=0 and a.type='p' and a.gid=0 order by a.sort desc`)
	if column != nil {
		wg := sync.WaitGroup{}
		for _, vv := range column {
			wg.Add(1)
			go func(v orm.Params) {
				defer wg.Done()
				id := v["id"].(string)
				var cids []string
				column2 := models.Query(`select id,name,hot from mt_column where status=1 and type='p' and pid=` + id + ` and gid=0 order by sort desc`)
				for _, v2 := range column2 {
					column3 := models.Query(`select id,name,hot from mt_column where status=1 and type='p' and pid=` + v2["id"].(string) + ` and gid=0  
					 order by sort desc`)
					for _, v3 := range column3 {
						cids = append(cids, v3["id"].(string))
					}
					v2["column3"] = column3
				}
				v["column2"] = column2
				v["brand"] = models.Query(`select a.id,b.pathname from mt_brand a left join mt_resources b on a.logo=b.id where a.status=1 and 
			a.censoring=1 and a.is_show=1 and a.cid like '%,` + id + `,%' order by a.recommend desc,a.sort desc`)
				cid := strings.Join(cids, ",")
				if fmt.Sprint(v["hot"]) == "1" {
					if cid == "" {
						cid = "''"
					}
					v["product"] = mobile.ProductList(`
					select a.id,a.isvip,a.title,a.price,a.sellnum,a.keywords,a.issellnum,b.pathname 
					from (select * from mt_product order by sort desc) a 
					left join mt_resources b on a.img=b.id 
					where a.status=1 and a.censoring=1 and a.is_show=1 and a.cid in(`+cid+`) `+shopWhere+` order by a.id desc limit 8`, c.Uid)
				}
			}(vv)
		}
		wg.Wait()
	}
	// c.Data["purchasing"] = models.Query(`select `)

	block := models.Query(`select a.*,b.pathname from mt_index_block a left join mt_resources b on a.img=b.id where ` + gbuyWhere + ` a.status=1 order by defaultsort`)
	wg := sync.WaitGroup{}
	for _, vv := range block {
		wg.Add(1)
		go func(v orm.Params) {
			defer wg.Done()
			if v["pid"] != nil && v["cid"] != nil {
				info := make(map[string]interface{})
				product := models.Query(`
				select a.id,a.title,a.sellnum,b.pathname,c.name as bname from (select * from mt_product order by recommend desc,sort desc) a 
				left join mt_resources b on a.img=b.id left join mt_brand c on c.id=a.bid
				where a.status=1 and a.censoring=1 and a.is_show=1 ` + shopWhere + ` and a.id in(` + v["pid"].(string) + `)  limit 0,` + v["pnum"].(string))
				for _, p := range product {
					p["link"] = `/product/content?id=` + p["id"].(string)
					price, _, _ := mobile.ProductPrice(p["id"].(string), c.Uid)
					//beego.Debug(price)
					//beego.Debug(price)
					p["price"] = price
				}

				info["url"] = `/indexblock?id=` + v["id"].(string)
				// beego.Debug(product)
				if product != nil {
					info["mainproduct"] = product[0]
					info["lastproduct"] = product[1:]
					info["product"] = product
					info["info"] = v

					c.Data["block"+v["defaultsort"].(string)] = info
				}
			}
		}(vv)
	}
	wg.Wait()
	var indexshow = make(map[string]interface{})
	indexshow["brandshow"] = true
	indexshow["proshow"] = true
	indexshow["noviceshow"] = true
	if gid != "0" {
		shop := models.Query("select brandshow,proshow,noviceshow from mt_shop where status=1 and censoring=1 and id=?", gid)
		if shop != nil {
			if shop[0]["brandshow"] != "1" {
				indexshow["brandshow"] = false
			}
			if shop[0]["proshow"] != "1" {
				indexshow["proshow"] = false
			}
			if shop[0]["noviceshow"] != "1" {
				indexshow["noviceshow"] = false
			}
		}
	}
	c.Data["indexshow"] = indexshow
	c.Data["gid"] = gid
	c.Data["column"] = column
	c.Data["good"] = mobile.ProductList(`
			select a.id,a.isvip,a.title,a.price,a.sellnum,a.keywords,a.issellnum,b.pathname 
			from (select * from mt_product order by sort desc) a 
			left join mt_resources b on a.img=b.id 
			where a.status=1 and a.censoring=1 and a.is_show=1 and a.good=1  `+shopWhere+` limit 0,5`, c.Uid)

	c.Data["recom"] = mobile.ProductList(`
		select a.id,a.isvip,a.title,a.price,a.sellnum,a.keywords,a.issellnum,b.pathname 
		from (select * from mt_product order by sort desc) a 
		left join mt_resources b on a.img=b.id 
		where a.status=1 and a.censoring=1 and a.is_show=1 and a.recom=1 `+shopWhere+` limit 0,5`, c.Uid)

	c.Data["mbuy"] = mobile.ProductList(`
		select a.id,a.isvip,a.title,a.price,a.sellnum,a.keywords,a.issellnum,b.pathname 
		from (select * from mt_product order by sort desc) a 
		left join mt_resources b on a.img=b.id 
		where a.status=1 and a.censoring=1 and a.is_show=1 and a.mbuy=1 `+shopWhere+` limit 0,5`, c.Uid)

	c.Data["recommend"] = mobile.ProductList(`
		select a.id,a.isvip,a.title,a.price,a.sellnum,a.keywords,a.issellnum,b.pathname 
		from  (select * from mt_product order by sort desc) a 
		left join mt_resources b on a.img=b.id 
		where a.status=1 and a.censoring=1 and a.is_show=1 and a.recommend=1 `+shopWhere+` limit 0,5`, c.Uid)

	c.Data["newproduct"] = mobile.ProductList(`
		select a.id,a.isvip,a.title,a.price,a.sellnum,a.keywords,a.issellnum,b.pathname 
		from (select * from mt_product order by addtime desc) a 
		left join mt_resources b on a.img=b.id 
		where a.status=1 and a.censoring=1 and a.is_show=1 `+shopWhere+` order by a.id desc limit 0,5`, c.Uid)

	nowtime := sword.Gettimes("")
	gbuy := models.Query(`select a.id,a.title,a.price,a.pid,a.endtime from mt_active_gbuy a where ` + gbuyWhere + ` a.status=1 and a.end=0 and 
		a.starttime<='` + nowtime + `' and a.endtime>'` + nowtime + `' order by 
		a.sort desc,a.addtime`)
	if gbuy != nil {
		wg := sync.WaitGroup{}
		for _, vv := range gbuy {
			wg.Add(1)
			go func(v orm.Params) {
				defer wg.Done()
				pid, ok := v["pid"].(string)
				if ok {
					product := mobile.ProductList(`select a.id,a.gid,a.title,b.pathname,a.keywords,a.price,a.isvip 
					from mt_product a 
					left join mt_resources b on a.img=b.id 
					where a.id=`+pid, c.Uid)
					if product != nil {
						v["product"] = product[0]
					}
				}
				endtime := fmt.Sprint(v["endtime"])
				nt := time.Now().Local().Unix()
				loc, _ := time.LoadLocation("Local")
				et, _ := time.ParseInLocation(Tparse, endtime, loc)
				res := et.Unix()
				v["dur"] = res - nt
			}(vv)
		}
		wg.Wait()
	}

	brandStreet := models.Query(`select name,bid from mt_brand_classify where status=1 limit 4`)
	if brandStreet != nil {
		for _, v := range brandStreet {
			if v["bid"] != nil && v["bid"] != "" {
				bid := v["bid"].(string)
				ctn := models.Query(`select a.id,a.name,b.pathname from mt_brand a 
					left join mt_resources b on a.logo=b.id 
					where a.id in(` + bid + `) and a.status=1 and a.censoring=1 and a.is_show=1 order by sort desc`)
				v["ctn"] = ctn
			}
		}
	}
	productStruct := models.Query(`select a.id,a.name,a.pid,a.introduce,b.pathname from mt_goods_struct a 
		left join mt_resources b on a.img=b.id 
		where a.status=1`)
	if productStruct != nil {
		wg := sync.WaitGroup{}
		for _, vv := range productStruct {
			wg.Add(1)
			go func(v orm.Params) {
				defer wg.Done()
				if v["pid"] != nil && v["pid"] != "" {
					pid := v["pid"].(string)
					var newpid = pid
					pids := strings.Split(pid, ",")
					if len(pids) > 11 {
						newpid = strings.Join(pids[:10], ",")
					}
					v["pros"] = models.Query(`select a.id,a.title,b.pathname from mt_product a 
					left join mt_resources b on a.img=b.id 
					where a.id in (` + newpid + `) and a.status=1 and a.censoring=1 and a.is_show=1 order by a.sort desc, a.addtime desc limit 5`)
				}
			}(vv)
		}
		wg.Wait()
	}
	noviceJoin := models.Query(`select a.id,a.name,b.pathname from mt_novice_column a 
		left join mt_resources b on a.img=b.id 
		where a.status=1`)
	if noviceJoin != nil {
		for _, v := range noviceJoin {
			id := v["id"].(string)
			v["ctn"] = models.Query(`select id,title from mt_novice_join where status=1 and cid=?`, id)
		}
	}
	c.Data["noviceJoin"] = noviceJoin
	c.Data["productStruct"] = productStruct
	c.Data["brandStreet"] = brandStreet
	c.Data["gbuy"] = gbuy
	c.Data["news"] = models.Query(`select a.id,a.title,b.pathname from mt_news a 
		left join mt_resources b on a.img=b.id 
		where a.status=1 and a.censoring=1 order by a.recommend desc,a.sort desc,a.addtime desc limit 5`)
	c.TplName = "pc/index/index.html"
}

type IndexArticle struct {
	BaseController
}

func (c *IndexArticle) Get() {
	id := c.GetString("id")
	models.Exec(`update mt_page set click=click+1 where id=` + id)
	c.Data["info"] = models.Query(`select a.*,b.name as cname from mt_page a left join mt_column b on a.cid=b.id where a.id=` + id)[0]
	c.TplName = "pc/index/article.html"
}

type NoviceArticle struct {
	BaseController
}

func (c *NoviceArticle) Get() {
	id := c.GetString("id")
	models.Exec(`update mt_novice_join set click=click+1 where id=?`, id)
	c.Data["info"] = models.Query(`select a.title,a.modifytime,a.content,a.click,b.name from mt_novice_join a 
		left join mt_novice_column b on a.cid=b.id where a.id=?`, id)[0]
	c.TplName = "pc/index/novice.html"
}

type IndexNews struct {
	BaseController
}

func (c *IndexNews) Get() {

	id := c.GetString("id")
	models.Exec(`update mt_news set click=click+1 where id=` + id)
	c.Data["info"] = models.Query(`select a.*,b.name as cname from mt_news a left join mt_column b on a.cid=b.id where a.id=` + id)[0]
	c.TplName = "pc/index/article.html"
}

type IndexShop struct {
	BaseController
}

func (c *IndexShop) Get() {
	id := c.GetString("id")
	if id == "" {
		return
	}
	var shop orm.Params
	if id == "0" {
		shop = models.Systems()
	} else {
		if shops := models.Query(`select a.id,a.name,b.pathname,c.pathname as gzhpath,d.pathname as apppath,e.pathname as webpath
		,f.pathname as xapppath from mt_shop a left join mt_resources b on a.logo=b.id left join mt_resources c on a.gzhimg=c.id 
		left join mt_resources d on a.appimg=d.id left join mt_resources e on a.webimg=e.id left join mt_resources f on a.xappimg=f.id 
		where a.status=1 and a.censoring=1 and a.id=` + id); shops != nil {
			shop = shops[0]
		} else {
			return
		}
	}
	now := time.Now().Local()
	nowtim2 := now.Format(Tparse)
	now.AddDate(0, -1, 0)
	nowtime := now.Format(Tparse)
	// shop["fnum"] = models.Query(`select count(a.id) as num from mt_member_focus a where a.gid=` + id)[0]["num"]
	shop["isfocus"] = 0
	if c.Uid != "" {
		if isfocus := models.Query(`select id from mt_member_focus where uid=` + c.Uid + ` and gid=` + id); isfocus != nil {
			shop["isfocus"] = 1
		}
	}
	var w []string
	if id == "0" {
		w = append(w, `(a.gid=0 or c.style=0)`)
	} else {
		w = append(w, `a.gid=`+id)
	}
	w = append(w, `a.status=1`)
	w = append(w, `a.censoring=1`)
	w = append(w, `a.is_show=1`)
	cid := c.GetString("cid")
	c.Data["cid"] = cid
	search := c.GetString("search")
	c.Data["search"] = search
	if search != "" {
		var w2 []string
		mtkeywords := models.Query(`select id from mt_keywords where title like '%` + search + `%' and status=1 and type=1`)
		w2 = append(w2, `a.title like '%`+search+`%'`)
		w2 = append(w2, `a.keywords like '%`+search+`%'`)
		// w2 = append(w2, `a.model like '%`+search+`%'`)
		// w2 = append(w2, `a.barcode like '%`+search+`%'`)
		for _, v := range mtkeywords {
			w2 = append(w2, `a.keywords like '%`+v["id"].(string)+`%'`)
		}
		//品牌
		w2s := strings.Join(w2, ` or `)
		if w2s != "" {
			w2s = `(` + w2s + `)`
			w = append(w, w2s)
		}
	} else {
		if cid != "" {
			if id == "0" {
				var cids []string
				cids = append(cids, cid)
				column := models.Query(`select id from mt_column where status=1 and pid=?`, cid)
				for _, v := range column {
					cids = append(cids, fmt.Sprint(v["id"]))
					column2 := models.Query(`select id from mt_column where status=1 and pid=?`, v["id"])
					for _, v2 := range column2 {
						cids = append(cids, fmt.Sprint(v2["id"]))
					}
				}
				cidss := strings.Join(cids, ",")

				w = append(w, `a.cid in(`+cidss+`)`)
			} else {
				w = append(w, `a.gcid=`+cid)
			}
		} else {
			wsnoc := strings.Join(w, ` and `)
			w = append(w, `a.addtime<'`+nowtime+`'`)
			w = append(w, `a.grecommend=0`)
			c.Data["news"] = mobile.ProductList(`select a.id,a.title,a.price,a.isvip,b.pathname,a.keywords,a.issellnum,a.sellnum,a.addtime 
	 from mt_product a 
	 left join mt_resources b on a.img=b.id 
	 left join mt_shop c on a.gid=c.id 
	 where `+wsnoc+` and a.addtime>'`+nowtime+`' order by a.addtime desc`, c.Uid)
			c.Data["rec"] = mobile.ProductList(`select a.id,a.title,a.price,a.isvip,b.pathname,a.keywords,a.issellnum,a.sellnum,a.addtime 
	 from mt_product a 
	 left join mt_resources b on a.img=b.id 
	 left join mt_shop c on a.gid=c.id 
	 where `+wsnoc+` and a.grecommend=1 order by a.addtime desc`, c.Uid)
		}
	}

	ob := ` order by a.gsort desc`
	ws := strings.Join(w, ` and `)
	page, page2, num := models.Pagechange(c.GetString("page"), "20")
	lt := ` limit ` + page + `,` + num
	sql := `select a.id,a.title,a.price,a.isvip,b.pathname,a.keywords,a.issellnum,a.sellnum,a.addtime 
	 from mt_product a 
	 left join mt_resources b on a.img=b.id 
	 left join mt_shop c on a.gid=c.id 
	 where ` + ws + ob + lt
	info := mobile.ProductList(sql, c.Uid)
	sql2 := `
		select count(a.id) as totals from mt_product a 
		left join mt_shop c on a.gid=c.id where ` + ws
	c.Data["paginator"] = models.Page(sql2, page2, num)

	column := models.Query(`select id,name from mt_column where gid=` + id + ` and type='p' and status=1 and pid=0`)

	coupon := models.Query(`select id,name,full,offer,effective,date_format(starttime,'%Y-%m-%d') as starttime,date_format(endtime,'%Y-%m-%d') 
				as endtime from mt_coupon where status=1 and starttime<='` + nowtim2 + `' and endtime>'` + nowtim2 + `' and gid=` + id)
	for _, v := range coupon {
		v["isget"] = 0
		if c.Uid != "" {
			if is := models.Query(`select id from mt_member_coupon where uid=` + c.Uid + ` and yid=` + v["id"].(string) + ` and status=1`); is != nil {
				v["isget"] = 1
			}
		}
	}

	score := models.Query(`select avg(score) as avgscore,avg(logisticsscore) as avglogisticsscore 
			from mt_shop_evaluation where gid=` + id)
	for _, v := range score {
		if avgscore, ok := v["avgscore"].(string); ok {
			avgscoref, _ := strconv.ParseFloat(avgscore, 64)
			v["avgscore"] = fmt.Sprintf(`%.1f`, avgscoref)
		} else {
			v["avgscore"] = 5
		}
		if logisticsscore, ok := v["logisticsscore"].(string); ok {
			logisticsscoref, _ := strconv.ParseFloat(logisticsscore, 64)
			v["logisticsscore"] = fmt.Sprintf(`%.1f`, logisticsscoref)
		} else {
			v["logisticsscore"] = 5
		}
	}
	c.Data["score"] = score[0]

	c.Data["coupon"] = coupon
	c.Data["gid"] = id
	c.Data["shop"] = shop
	c.Data["info"] = info
	c.Data["column"] = column
	c.Data["url"] = "/shop?id=" + id + "&cid=" + cid + "&search=" + search + "&"
	c.TplName = "pc/index/shop.html"
}

type IndexShopSales struct {
	BaseController
}

func (c *IndexShopSales) Get() {
	id := c.GetString("id")
	if id == "" {
		return
	}
	var shop orm.Params
	if id == "0" {
		shop = models.Systems()
	} else {
		if shops := models.Query(`select a.id,a.name,b.pathname,c.pathname as gzhpath,d.pathname as apppath,e.pathname as webpath
		,f.pathname as xapppath from mt_shop a left join mt_resources b on a.logo=b.id left join mt_resources c on a.gzhimg=c.id 
		left join mt_resources d on a.appimg=d.id left join mt_resources e on a.webimg=e.id left join mt_resources f on a.xappimg=f.id 
		where a.status=1 and a.censoring=1 and a.id=` + id); shops != nil {
			shop = shops[0]
		} else {
			return
		}
	}
	now := time.Now().Local()
	nowtim2 := now.Format(Tparse)
	// now.AddDate(0, -1, 0)
	// nowtime := now.Format(Tparse)
	// shop["fnum"] = models.Query(`select count(a.id) as num from mt_member_focus a where a.gid=` + id)[0]["num"]
	shop["isfocus"] = 0
	if c.Uid != "" {
		if isfocus := models.Query(`select id from mt_member_focus where uid=` + c.Uid + ` and gid=` + id); isfocus != nil {
			shop["isfocus"] = 1
		}
	}
	sql := `select a.id,a.title,a.price,a.isvip,b.pathname,a.keywords,a.issellnum,a.sellnum,a.addtime 
	from mt_active_sales d 
	left join mt_product a on a.id=d.pid 
	left join mt_resources b on a.img=b.id 
	left join mt_shop c on a.gid=c.id 
	where d.status=1 and d.end=0 and d.starttime<'` + nowtim2 + `' and d.endtime>'` + nowtim2 + `' and d.gid=` + id + ` 
	order by d.sort desc`
	info := mobile.ProductList(sql, c.Uid)

	column := models.Query(`select id,name from mt_column where gid=` + id + ` and type='p' and status=1 and pid=0`)

	score := models.Query(`select avg(score) as avgscore,avg(logisticsscore) as avglogisticsscore 
			from mt_shop_evaluation where gid=` + id)
	for _, v := range score {
		if avgscore, ok := v["avgscore"].(string); ok {
			avgscoref, _ := strconv.ParseFloat(avgscore, 64)
			v["avgscore"] = fmt.Sprintf(`%.1f`, avgscoref)
		} else {
			v["avgscore"] = 5
		}
		if logisticsscore, ok := v["logisticsscore"].(string); ok {
			logisticsscoref, _ := strconv.ParseFloat(logisticsscore, 64)
			v["logisticsscore"] = fmt.Sprintf(`%.1f`, logisticsscoref)
		} else {
			v["logisticsscore"] = 5
		}
	}
	c.Data["score"] = score[0]

	c.Data["gid"] = id
	c.Data["shop"] = shop
	c.Data["info"] = info
	c.Data["column"] = column
	c.Data["cid"] = ""
	c.Data["search"] = ""
	c.TplName = "pc/index/shop_sales.html"
}
func (c *IndexShop) Post() {
	defer c.ServeJSON()
	if c.Uid == "" {
		c.Data["json"] = 2
	} else {
		id := c.GetString("id")
		if id != "" {
			if is := models.Query(`select id from mt_member_focus where uid=` + c.Uid + ` and gid=` + id); is != nil {
				if err := models.Exec(`delete from mt_member_focus where id=` + is[0]["id"].(string)); err != nil {
					beego.Debug(err)
					c.Data["json"] = 0
				} else {
					c.Data["json"] = 1
				}
			} else {
				if err := models.Exec(`insert into mt_member_focus (uid,gid,addtime) values (` + c.Uid + `,` + id + `,'` + sword.Gettimes("") + `')`); err != nil {
					beego.Debug(err)
					c.Data["json"] = 0
				} else {
					c.Data["json"] = 1
				}
			}
		}
	}
}

type IndexProgram struct {
	BaseController
}

func (c *IndexProgram) Get() {
	var column []orm.Params
	shopWhere := ""
	if c.ShopId != "" {
		shopWhere = "a.gid = " + c.ShopId + " and"
	}
	id := c.GetString("id")
	info := models.Query(`select * from mt_program where id=?`, id)[0]
	cid := fmt.Sprint(info["cid"])
	pid := fmt.Sprint(info["pid"])
	if cid != "" && pid != "" {
		column = models.Query(`select id,name from mt_column where id in(` + cid + `) order by sort desc`)
		for _, v := range column {
			var cids []string
			if c.ShopId != "" {
				cids = append(cids, fmt.Sprint(v["id"]))
			}
			columnson := models.Query(`select id from mt_column where status=1 and pid=?`, v["id"])
			for _, s := range columnson {
				cids = append(cids, fmt.Sprint(s["id"]))
				columnson2 := models.Query(`select id from mt_column where status=1 and pid=?`, s["id"])
				for _, s2 := range columnson2 {
					cids = append(cids, fmt.Sprint(s2["id"]))
				}
			}
			cidss := strings.Join(cids, ",")
			product := ProductSearch(`
				select a.id,a.title,a.number,a.price,a.model,a.number,a.minbuy,a.length,a.height,a.width,a.weight,b.pathname,a.isvip,c.name as bname  
				from mt_product a 
				left join mt_resources b on a.img=b.id 
				left join mt_brand c on a.bid=c.id 
				where `+shopWhere+` a.censoring=1 and a.status=1 and a.is_show=1 and a.id in(`+pid+`) and a.cid in (`+cidss+`) 
				order by a.sort desc`, c.Uid)
			// now := tcsc/sword.Gettimes("")
			// for _, p := range product {
			// 	gbuy := models.Query(`select min from mt_active_gbuy where end=0 and pid=` + p["id"].(string) + ` and status=1 and starttime<='` + now + `' and endtime>'` + now + `'
			// 		order by addtime desc`)
			// 	if gbuy != nil {
			// 		p["min"] = gbuy[0]["min"].(string)
			// 	}
			// 	price, _, _ := mobile.ProductPrice(p["id"].(string), c.Uid)
			// 	// number := fmt.Sprint(p["number"])
			// 	// price = tcsc/sword.Mult(price, number)
			// 	p["price"] = price
			// }
			v["product"] = product
		}
	}
	if c.IsAjax() {
		defer c.ServeJSON()
		var js = make(map[string]interface{})
		c.Data["json"] = &js
		js["column"] = column
		js["info"] = info
	} else {
		c.Data["column"] = column
		c.Data["title"] = info["name"]
		c.TplName = "pc/index/program.html"
	}
}

type IndexGood struct {
	BaseController
}

func (c *IndexGood) Get() {
	// var column []orm.Params
	shopWhere := ""
	if c.ShopId != "" {
		shopWhere = "a.gid = " + c.ShopId + " and"
	}
	id := c.GetString("id")
	s := models.Query(`select * from mt_goods_struct where status=1 and id=?`, id)
	product := models.Query(`
				select a.id,a.title,a.number,a.price,a.model,a.number,a.minbuy,a.length,a.height,a.width,a.weight,b.pathname,c.name as bname  
				from mt_product a 
				left join mt_resources b on a.img=b.id 
				left join mt_brand c on a.bid=c.id 
				where ` + shopWhere + ` a.censoring=1 and a.status=1 and a.is_show=1 and a.id in(` + fmt.Sprint(s[0]["pid"]) + `)  
				order by a.cid`)
	now := sword.Gettimes("")
	for _, p := range product {
		gbuy := models.Query(`select min from mt_active_gbuy where end=0 and pid=` + p["id"].(string) + ` and status=1 and starttime<='` + now + `' and endtime>'` + now + `' 
					order by addtime desc`)
		if gbuy != nil {
			p["min"] = gbuy[0]["min"].(string)
		}
		price, _, _ := mobile.ProductPrice(p["id"].(string), c.Uid)
		// number := fmt.Sprint(p["number"])
		// price = tcsc/sword.Mult(price, number)
		p["price"] = price
	}

	if c.IsAjax() {
		defer c.ServeJSON()
		var js = make(map[string]interface{})
		c.Data["json"] = &js
		js["info"] = product
	} else {
		c.Data["title"] = s[0]["name"]
		c.Data["product"] = product
		c.TplName = "pc/index/good.html"
	}
}

type IndexBlock struct {
	BaseController
}

func (c *IndexBlock) Get() {
	var column []orm.Params
	shopWhere := ""
	if c.ShopId != "" {
		shopWhere = "a.gid = " + c.ShopId + " and"
	}
	id := c.GetString("id")
	info := models.Query(`select * from mt_index_block where id=` + id)[0]
	cid := fmt.Sprint(info["cid"])
	pid := fmt.Sprint(info["pid"])
	if cid != "" && pid != "" {
		column = models.Query(`select id,name from mt_column where id in(` + cid + `)`)
		for i, v := range column {
			var cids []string
			if c.ShopId != "" {
				cids = append(cids, fmt.Sprint(v["id"]))
			}
			columnson := models.Query(`select id from mt_column where status=1 and pid=?`, v["id"])
			for _, s := range columnson {
				cids = append(cids, fmt.Sprint(s["id"]))
				columnson2 := models.Query(`select id from mt_column where status=1 and pid=?`, s["id"])
				for _, s2 := range columnson2 {
					cids = append(cids, fmt.Sprint(s2["id"]))
				}
			}
			cidss := strings.Join(cids, ",")
			product := models.Query(`
				select a.id,a.title,a.price,b.pathname from mt_product a 
				left join mt_resources b on a.img=b.id 
				where ` + shopWhere + ` a.censoring=1 and a.status=1 and a.is_show=1 and a.id in(` + pid + `) and a.cid in(` + cidss + `)`)
			if product != nil {
				for _, p := range product {
					price, _, _ := mobile.ProductPrice(p["id"].(string), c.Uid)
					p["price"] = price
				}
				v["product"] = product
			} else {
				column[i] = nil
			}
		}
	}
	c.Data["column"] = column
	c.Data["title"] = info["name"]
	c.TplName = "pc/index/block.html"
}

type IndexBrand struct {
	BaseController
}

func (c *IndexBrand) Get() {
	c.Data["brand"] = models.Query(`select a.id,b.pathname,a.name from mt_brand a left join mt_resources b on a.logo=b.id where a.status=1 and 
		a.censoring=1 and a.is_show=1 order by a.recommend desc,a.sort desc`)
	c.TplName = "pc/index/brandlist.html"
}

func (c *IndexProgram) Post() {
	defer c.ServeJSON()
	if c.Uid == "" {
		c.Data["json"] = 962
		return
	}
	c.Data["json"] = 1
	ids := c.GetStrings("id")
	nums := c.GetStrings("num")
	nows := sword.Gettimes("")
	for k, v := range ids {
		price, gid, tid := mobile.ProductPrice(v, c.Uid)
		if ishave := models.Query(`
			select id 
			from mt_order_car 
			where status=1 and uid=? and pid=?`, c.Uid, v); ishave != nil {
			if err := models.Exec(`
				update mt_order_car 
				set price=?,num=num+? 
				where id=?`, price, nums[k], ishave[0]["id"]); err != nil {
				beego.Debug(err)
				c.Data["json"] = 0
				return
			}
		} else {
			if err := models.Exec(`
				insert into mt_order_car (gid,uid,pid,num,price,status,addtime,type) 
				values (?,?,?,?,?,1,?,?)`, gid, c.Uid, v, nums[k], price, nows, tid); err != nil {
				beego.Debug(err)
				c.Data["json"] = 0
				return
			}
		}
	}
}
func (c *IndexGood) Post() {
	defer c.ServeJSON()
	if c.Uid == "" {
		c.Data["json"] = 962
		return
	}
	c.Data["json"] = 1
	ids := c.GetStrings("id")
	nums := c.GetStrings("num")
	nows := sword.Gettimes("")
	for k, v := range ids {
		price, gid, tid := mobile.ProductPrice(v, c.Uid)
		if ishave := models.Query(`
			select id 
			from mt_order_car 
			where status=1 and uid=? and pid=?`, c.Uid, v); ishave != nil {
			if err := models.Exec(`
				update mt_order_car 
				set price=?,num=num+? 
				where id=?`, price, nums[k], ishave[0]["id"]); err != nil {
				beego.Debug(err)
				c.Data["json"] = 0
				return
			}
		} else {
			if err := models.Exec(`
				insert into mt_order_car (gid,uid,pid,num,price,status,addtime,type) 
				values (?,?,?,?,?,1,?,?)`, gid, c.Uid, v, nums[k], price, nows, tid); err != nil {
				beego.Debug(err)
				c.Data["json"] = 0
				return
			}
		}
	}
}

type ExcelUoload struct {
	BaseController
}

func (c *ExcelUoload) Post() {
	defer c.ServeJSON()
	var js = make(map[string]interface{})
	c.Data["json"] = &js
	f, n, errup := c.GetFile("file")
	if errup != nil {
		beego.Debug("上传文件失败:", errup)
		js["status"] = 0
		return
	}
	defer f.Close()
	filename := n.Filename
	dirpath := "static/slim/xlsx"
	if errdir := os.MkdirAll(dirpath, 0777); errdir != nil {
		beego.Debug(errdir)
		js["status"] = 0
		return
	}
	err := c.SaveToFile("file", dirpath+"/"+filename)
	if err != nil {
		beego.Debug("insert media数据失败:", err)
		js["status"] = 0
		return
	}
	xlsx, err := excelize.OpenFile(dirpath + "/" + filename)
	if err != nil {
		beego.Debug("insert media数据失败:", err)
		js["status"] = 0
		return
	}
	rows := xlsx.GetSheetMap()
	nows := sword.Gettimes("")
	var nothave []string
	for _, v := range rows {
		row := xlsx.GetRows(v)
		for _, r := range row {
			pid := models.Query(`select a.id from mt_product a left join mt_shop b on a.gid=b.id 
				where a.status=1 and a.censoring=1 and a.is_show=1 and (a.gid=0 or b.style=0) and a.barcode=?`, r[1])
			if pid != nil {
				if ishave := models.Query(`select id from mt_order_car where uid=? and pid=? and status=1 and type<>1`, c.Uid, pid[0]["id"]); ishave != nil {
					if err := models.Exec(`update mt_order_car set num=? where id=?`, r[2], ishave[0]["id"]); err != nil {
						beego.Debug(err)
					}
				} else {
					price, _, tid := mobile.ProductPrice(fmt.Sprint(pid[0]["id"]), c.Uid)
					var t = 0
					if tid != "0" {
						t = 2
					}
					if err := models.Exec(`insert into mt_order_car (pid,gid,num,uid,price,addtime,spec,type,status) values (?,0,?,?,?,?,?,?,1)`, pid[0]["id"], r[2], c.Uid, price, nows, 0, t); err != nil {
						beego.Debug(err)
					}
				}
			} else {
				nothave = append(nothave, r[0])
			}
		}
	}
	js["status"] = 1
	var msg string
	if len(nothave) == 0 {
		msg = "导入成功"
	} else {
		nothaves := strings.Join(nothave, ",")
		msg = nothaves + "未导入"
	}
	js["msg"] = msg
}

type PrivacyAgreement struct {
	BaseController
}

func (c *PrivacyAgreement) Get() {
	c.TplName = "pc/index/privacyAgreement.html"
}
